package com.hefu.virtuallife.customview.ablgrid;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.hefu.virtuallife.R;
import com.hefu.virtuallife.bean.SmartHomeBean;
import com.hefu.virtuallife.utils.SmartDataUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by pc on 2018/7/26.
 */



public class AblGridViewBaseAdapter1 extends BaseAdapter {

    private Context mContext;
    private String[] scenes;
    private Map<String, List<SmartHomeBean>> sceneMap;
    private int[] sceneBg;


    public AblGridViewBaseAdapter1(Context context, String[] scenes, int[] sceneBg, Map<String, List<SmartHomeBean>> sceneMap) {
        super();
        mContext = context;
        this.scenes = scenes;
        this.sceneBg = sceneBg;
        this.sceneMap = sceneMap;
    }

    @Override
    public int getCount() {
        return sceneMap.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = loadData(position, convertView, parent);
        return convertView;
    }

    public View loadData(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.smart_scene_item, parent, false);
        }
        TextView tv_scene = get(convertView, R.id.tv_scene);
        ImageView scene_bg = get(convertView, R.id.scene_bg);
        TableLayout tl = get(convertView, R.id.tl_appliances);

        String scene = scenes[position];

        scene_bg.setBackgroundResource(sceneBg[position]);
        tv_scene.setText(scene);

        TableRow tableRow1 = new TableRow(mContext);
        tableRow1.setWeightSum(1.0f);
        TableRow tableRow2 = new TableRow(mContext);
        tableRow2.setWeightSum(1.0f);
        List<SmartHomeBean> smarts = sceneMap.get(scene);
        int size = smarts.size();

        for (int i  = 0; i<size; i++){
            SmartHomeBean smart = smarts.get(i);
            TextView tv = new TextView(mContext);
            tv.setText(smart.getName());
            Drawable drawable = mContext.getResources().getDrawable(SmartDataUtils.getRes(smart.getType()));
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            tv.setCompoundDrawables(null, drawable, null, null);
            if ((i/3) < 1){
                tableRow1.addView(tv);
            } else{
                tableRow2.addView(tv);
            }
        }
        tl.addView(tableRow1);
        tl.addView(tableRow2);

        return convertView;
    }

    public <T extends View> T get(View view, int id) {
        SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();
        if (viewHolder == null) {
            viewHolder = new SparseArray<View>();
            view.setTag(viewHolder);
        }
        View childView = viewHolder.get(id);
        if (childView == null) {
            childView = view.findViewById(id);
            viewHolder.put(id, childView);
        }
        return (T) childView;
    }

}
